<?php
include"../inc/config.inc.php";

$id = $_POST['id'];
$op = $_POST['op'];

if($op == 'add1')
{
    $get_year = (date("Y")+543);
    for($i=0; $i<count($id); $i++)
    {
        $sql = "SELECT std_id, level_id, year_id"
            ." FROM student"
            ." WHERE std_id = '".$id[$i]."'";
        $result = mysql_query($sql);
        $row = mysql_fetch_object($result);

        $sql = "SELECT b.year"
            ." FROM student AS a,"
            ." year AS b"
            ." WHERE a.std_id = '".$id[$i]."'"
            ." AND a.year_id = b.year_id"
            ;
        $result = mysql_query($sql);
        $rowy = mysql_fetch_object($result);

        $year = (int)$rowy->year;
        if($year < $get_year)
        {
            $arr1 = str_split($row->level_id);
            $arr2 = str_split($row->year_id);

            $test = (int) $arr2[1].$arr2[2];
            $number = (int) $arr1[1];

            if($arr1[1] <= 6)
            {
                $new_level = "L".($number+1);
                $years = "Y".($test+1);
            }
            else
            {
                $new_level = $row->level_id;
                $years = $row->year_id;
            }

            $sql_update = "UPDATE student set level_id = '".$new_level."',"
                         ." year_id = '".$years."',"
                         ." level_store = 1"
                         ." WHERE std_id = '".$id[$i]."'";
            mysql_query($sql_update);

            $insert = "INSERT INTO uplevel (std_id ,level_id ,year_id )"
                     ." VALUES ('".$row->std_id."', '".$row->level_id."', '".$row->year_id."');";
            mysql_query($insert);

        }
    }// End foreach
    echo"<meta http-equiv='refresh' content='0;URL=student.php'>";
}
else if($op == 'add2')
{
    
    for($i=0; $i<count($id); $i++)
    {
        $sql_update = "UPDATE student set level_store = 0"
                     ." WHERE std_id = '".$id[$i]."'";
        mysql_query($sql_update);
        echo"<meta http-equiv='refresh' content='0;URL=student.php'>";
        
    }// End foreach
}
?>